package com.aibao.serialportlibrary.thread;

import android.text.TextUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 串口消息读取线程
 */

public abstract class SerialPortReadThread extends Thread {

    public abstract void onDataReceived(String weight);

    private static final String TAG = SerialPortReadThread.class.getSimpleName();
    private InputStream mInputStream;
    private byte[] mReadBuffer;
    private final int bufferSize = Byte.MAX_VALUE;

    public SerialPortReadThread(InputStream inputStream) {
        mInputStream = inputStream;
        mReadBuffer = new byte[1024];
    }

    @Override
    public void run() {
        super.run();

        while (!isInterrupted()) {
            try {
                if (null == mInputStream) {
                    return;
                }
                Thread.sleep(50);
                int size = mInputStream.read(mReadBuffer);

                if (mInputStream == null) return;
                if (size == bufferSize) {
                    continue;
                }
                if (size > 0) {
                    onDataHandler(mReadBuffer, size);
                }

            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
    }

    /**
     * 爱宝、顶尖
     */
//    private Pattern p1  = Pattern.compile("((?<=S ?)(-?\\d+\\.?\\d+)(?=kg))");
    private Pattern p1 = Pattern.compile("((?<=S|U?)(-?\\d+\\.?\\d+)(?=(kg.|LB.|Lb.|\\\\sg.)))");
    /**
     * 大华
     */
    private Pattern p2 = Pattern.compile("(?<=\\s)\\d{4,}");
    private void onDataHandler(final byte[] buffer, final int size) {
        String content = null;
        String received = new String(buffer, 0, size);

//        顶尖、爱宝
        Matcher m = p1.matcher(received);
        while (m.find()) {
            content = m.group();
        }
//        /**
//         * 适配新协议F1-3
//         */
//        if (TextUtils.isEmpty(content)) {
//            if (received.length() >= 16) {
//                if (received.contains("S") && (received.contains("kg") || received.contains("g"))) {
//                    int indexS = received.indexOf("S");
////                    int indexKg = received.indexOf("kg") + 1;
//                    int indexKg = indexS + 1 + 1 + 1 + 6;//结束位下标需要+1、一位标志位、从标志位下一位、取6位（实际数据包含小数点是6位）
//                    content = received.substring(indexS + 1, indexKg);
//                    content = String.valueOf(Double.valueOf(content));
//                }
//            }
//        }

        // 没有就判断大华
        if (TextUtils.isEmpty(content)) {
            Matcher m2 = p2.matcher(received);
            while (m2.find()) {
                content = m2.group();
            }
            if (!TextUtils.isEmpty(content)) {
                content = String.valueOf(Float.parseFloat(content) / 1000.0);
            }
        }

        if (!TextUtils.isEmpty(content)) {
            onDataReceived(content);
        }
    }

    @Override
    public synchronized void start() {
        super.start();
    }

    /**
     * 关闭线程 释放资源
     */
    public void release() {
        interrupt();

        if (null != mInputStream) {
            try {
                mInputStream.close();
                mInputStream = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
